package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.authoriseRFC;

import java.sql.ResultSet;
import java.util.ArrayList;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.RFCMaker;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.authorizeRFCPanel.AuthorizeRFCPanel;

public class LoadreadyforApprovalRFCThread extends Thread {

	private AuthorizeRFCPanel authorizeRFCPanel;
	private DatabaseConnection connection;

	public LoadreadyforApprovalRFCThread(AuthorizeRFCPanel authorizeRFCPanel) {
		this.authorizeRFCPanel = authorizeRFCPanel;
	}

	@Override
	public void run() {

		authorizeRFCPanel.getListPanel().getDisplayTable().setVisible(false);
		this.connection = new DatabaseConnection();
		connection.connect();

		String query = "SELECT * FROM CMS_RFC WHERE " + RFC.TC_Status
				+ " = 'ready for approval'";
		ResultSet rs = connection.query(query);

		authorizeRFCPanel.getListPanel().getDisplayTable().clearTable();

		ArrayList<RFC> rfcs = new ArrayList<RFC>();
		int count = 0;
		try {
			while (rs.next()) {
				RFCMaker maker = new RFCMaker();

				rfcs.add(maker.makeanRFC(rs));

				count++;
			}
		} catch (Exception e) {
		}

		if (count == 0) {

			this.authorizeRFCPanel.getListPanel().getDisplayTable().displayNoElenemt();
		} else {

			this.authorizeRFCPanel.getListPanel().getDisplayTable()
					.setVisible(false);
			for (RFC rfc : rfcs) {
				this.authorizeRFCPanel.getListPanel().getDisplayTable()
						.addRFCinTable(rfc);
			}
			this.authorizeRFCPanel.getListPanel().getDisplayTable()
					.setVisible(true);
		}
		connection.disconnect();
		authorizeRFCPanel.getFunctionPanel().getBu_showFRC().setEnabled(true);
		authorizeRFCPanel.getFunctionPanel().getProgrss().setVisible(false);
		authorizeRFCPanel.getListPanel().getDisplayTable().setVisible(true);
	}

}
